package com.xyd.servlet;

import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.xyd.entity.Product;
import com.xyd.service.ProductSericve;
import com.xyd.service.impl.ProductServiceImpl;
import com.xyd.util.IpTimeStamp;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 类描述：
 *
 * @author 张吉哲
 * @date 2021/1/12 9:42
 */
@WebServlet("/productAdminAddServlet")
public class ProductAdminAddServlet extends HttpServlet {

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // 服务器保存路径
    String filePath = "images/upload";
    boolean result = true;

    File file = new File(filePath);
    if (file.exists()) {
      file.mkdir();
    }
    //实例化上传组件
    SmartUpload su = new SmartUpload();
    try {
      //初始化SmartUpload
      su.initialize(getServletConfig(), request, response);
      //设置上传文件对象10M
      su.setMaxFileSize(1024 * 1024 * 10);
      //设置所有文件大小100M
      su.setTotalMaxFileSize(1024 * 1024 * 100);
      //设置允许上传文件类型
      su.setAllowedFilesList("jpg,gif,png");

      //设置禁止上传文件类型
      su.setDeniedFilesList("rar,jsp,js,txt");
      su.setCharset("UTF-8");
      //上传文件
      su.upload();
      Product product = null;
      Request req = su.getRequest();
      if (su.getFiles().getFile(0).getFileName().length() > 0) {

        IpTimeStamp ipTimeStamp = new IpTimeStamp(request.getRemoteAddr());
        String fileExt = su.getFiles().getFile(0).getFileExt();
        String fileName = ipTimeStamp.getIPTimeRand() + "." + fileExt;
        //保存文件
        su.getFiles().getFile(0).saveAs(filePath + File.separator + fileName);

        String epIdStr = req.getParameter("epId");
        Long epId = null;
        if (epIdStr != null && epIdStr.length() > 0) {
          epId = Long.parseLong(epIdStr);
        }

        String epPrice_str = req.getParameter("productPrice");
        Double price = null;
        if (epPrice_str != null && epPrice_str.length() > 0) {
          price = Double.parseDouble(epPrice_str);
        }
        System.out.println(req.getParameter("parentId"));
        product = new Product(
            epId,
            req.getParameter("epName"),
            req.getParameter("epDescription"),
            price,
            Integer.parseInt(req.getParameter("epStock")),
            Long.parseLong(req.getParameter("parentId")),
            null,
            (filePath + File.separator + fileName), null, null
        );
      } else {
        String epIdStr = req.getParameter("epId");
        Long epId = null;
        if (epIdStr != null && epIdStr.length() > 0) {
          epId = Long.parseLong(epIdStr);
        }

        product = new Product(
            epId,
            req.getParameter("epName"),
            req.getParameter("epDescription"),
            Double.parseDouble(req.getParameter("productPrice")),
            Integer.parseInt(req.getParameter("epStock")),
            Long.parseLong(req.getParameter("parentId"))
        );
      }
      ProductSericve productSericve = new ProductServiceImpl();
      if (product.getEpId() != null) {
        Boolean boo = productSericve.adminChangeProduct(product);
        response
            .sendRedirect(
                request.getContextPath() + "/manage/product-modify.html?epId=" + product.getEpId()
                    + "&result=" + boo);
        return;
      } else {
        result = productSericve.adminAddProduct(product);
      }
    } catch (Exception e) {
      result = false;
      e.printStackTrace();
    }

    response.sendRedirect(request.getContextPath() + "/manage/product-add.html?result=" + result);
  }

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    doPost(request, response);
  }
}
